// 原创作品

import 'package:flutter/material.dart';
import 'package:sharing/resources/styles.dart';
import 'package:sharing/ui/user_info_page.dart';
import 'package:sharing/ui/widget/origin_article_page.dart';
import 'package:sharing/model/origin_article.dart';

class OriginWork extends StatelessWidget {
  OriginWork({@required this.originArticle}) : assert(originArticle != null);
  final OriginArticle originArticle;

  Widget _buildHeader(BuildContext context) {
    return GestureDetector(
      onTap: () {
        print("用户头像的点击事件");
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) => UserInfoPage(
                  user: originArticle.user,
                ),
          ),
        );
      },
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            CircleAvatar(
              radius: 18,
              backgroundImage: NetworkImage(originArticle.user.avatar),
//              child: Image.network(
//                  originArticle.coverUrl,
//                  fit: BoxFit.cover),
            ),
            Expanded(
              child: Container(
                margin: const EdgeInsets.only(left: 8.0),
                child: Text(
                  originArticle.user.name,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: AppStyles.userInfoTextStyle,
                ),
              ),
            ),
            Text(originArticle.pushDate),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.all(4.0),
      child: Container(
        padding: const EdgeInsets.all(4.0),
        child: GestureDetector(
          onTap: () {
            print("查看文章...");
            Navigator.of(context).push(
              MaterialPageRoute(
                builder: (context) => OriginArticlePage(article: originArticle,),
              ),
            );
          },
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              _buildHeader(context),
              SizedBox(
                height: 4.0,
              ),
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 8.0),
                child: Text(
                  originArticle.title,
                  textAlign: TextAlign.left,
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  style: AppStyles.bookTitleTextStyle,
                ),
              ),
              SizedBox(
                height: 4.0,
              ),
              Container(
                padding:
                    const EdgeInsets.symmetric(horizontal: 4.0, vertical: 1.0),
                height: 144,
                child: Image.network(
                  originArticle.coverUrl,
                  fit: BoxFit.fill,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
